# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp import netsvc
from openerp.tools.float_utils import float_compare
#----------------------------------------------------------
# Project Task
#----------------------------------------------------------

class task(osv.osv):

    _inherit = "project.task"

    _columns = {
        'user_ids': fields.many2many('res.users', 'project_task_users_rel', 'task_id', 'user_id', 'Assigned to'),
    }

    _defaults = {
        'reviewer_id': False,
        'user_id': False,
    }

class project(osv.osv):

    _inherit = "project.project"

    def set_cancel(self, cr, uid, ids, context=None):
        project_project = self.browse(cr, uid, ids, context=context)
        if project_project.analytic_account_id:
            self.pool.get('account.analytic.account').write(cr,uid, [project_project.analytic_account_id.id],{'state': 'cancelled'}, context=context)
        return self.write(cr, uid, ids, {'state': 'cancelled'}, context=context)

    def set_open(self, cr, uid, ids, context=None):
        project_project = self.browse(cr, uid, ids, context=context)
        if project_project.analytic_account_id:
            self.pool.get('account.analytic.account').write(cr,uid, [project_project.analytic_account_id.id],{'state': 'open'}, context=context)      
        return self.write(cr, uid, ids, {'state': 'open'}, context=context)
 
    def set_done(self, cr, uid, ids, context=None):
        project_project = self.browse(cr, uid, ids, context=context)
        if project_project.analytic_account_id:
            self.pool.get('account.analytic.account').write(cr,uid, [project_project.analytic_account_id.id],{'state': 'close'}, context=context) 
        return self.write(cr, uid, ids, {'state': 'close'}, context=context)
 
    def unlink(self, cr, uid, ids, context=None):

        raise osv.except_osv(
            _('Invalid Action!'),
            _('you can not delete the Project except in Cancel.')
        )

        return super(project, self).unlink(cr, uid, ids, context=context)
 
 
class project_work(osv.osv):
    
    _inherit = "project.task.work"

    _columns = {
        'work_summary': fields.many2one('project.task.work.summary', 'Work Summary'),
        'type':fields.selection([('normal','Normal'),
                                 ('ot','OT'),
                                 ], 'Type', required=True),
        'department':fields.selection([('shipyard','Shipyard'),
                                 ('mechanical','Mechanical'),
                                 ('electrical','Electrical'),
                                 ('welding_fabrication','Welding & Fabrication'),
                                 ('interior','Interior'),
                                 ('design','Design'),
                                 ('other_dept1','Other Dept1'),
                                 ('other_dept2','Other Dept2'),
                                 ('other_dept3','Other Dept3')
                                 ], 'Department', required=True),
    }
    _defaults = {
        'type': 'normal',
    }

    def create(self, cr, uid, vals, *args, **kwargs):
        context = kwargs.get('context', {})
        context['no_analytic_entry'] = True
#         print context
#         raise osv.except_osv(
#             _('Invalid Action Test!'),
#             _('you can not delete the Project except in Cancel.')
#         )
        return super(project_work,self).create(cr, uid, vals, *args, **kwargs)

class project_task_work_summary(osv.osv):
    _name = 'project.task.work.summary'
    _description = 'Project Task Work Summary'

    _columns = {
        'name': fields.char('Name', required=True, select=True),
        'description': fields.text('Description'),
    }
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
